//
//  ArtworkCollectionViewCell.m
//  jingdongfang
//
//  Created by MTiMac01 on 2018/1/8.
//  Copyright © 2018年 LJL. All rights reserved.
//

#import "ArtworkCollectionViewCell.h"
#import "SotreTitleCell.h"
#import "StoreDeatilVC.h"
@interface ArtworkCollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@end

@implementation ArtworkCollectionViewCell
- (void)layoutSubviews
{
    [super layoutSubviews];
    [self addSubview:self.collectionView];
}

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
        [flow setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH,40) collectionViewLayout:flow];
        self.collectionView.backgroundColor = [UIColor c2Color];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.showsVerticalScrollIndicator = YES;//垂直方向
        self.collectionView.showsHorizontalScrollIndicator = NO;//水平方向
        [self.collectionView registerClass:[SotreTitleCell class] forCellWithReuseIdentifier:@"SotreTitleCell"];
    }
    return _collectionView;
}

- (void)setDataArr:(NSMutableArray *)dataArr
{
    if (_dataArr != dataArr)
    {
        _dataArr = dataArr;
        [self.collectionView reloadData];
    }
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count > 0 ? self.dataArr.count : 0;
}

//设置item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = _dataArr[indexPath.item][@"title"];
    CGFloat width = [Tool widthForLabel:[NSString stringWithFormat:@"%@",str] fontSize:13];
    return CGSizeMake(width + 30, 40);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SotreTitleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SotreTitleCell" forIndexPath:indexPath];
    cell.str = _dataArr[indexPath.item][@"title"];
    return cell;
}

//设置分区边界
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 15, 0, 15);
}
#pragma mark --- item 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    StoreDeatilVC *vc = [[StoreDeatilVC alloc] init];
    vc.str = self.dataArr[indexPath.row][@"title"];
    vc.type = @"2";
    vc.value = self.dataArr[indexPath.row][@"id"];
    [UIViewController ig_pushViewController:vc animated:YES];
}

@end
